import {
  addOrganization,
  putOrganization,
  getOrgList,
  getMemberList,
  addOrgMember,
  editOrgMember,
  delOrgMember,
  changeOrganization,
  importMailList,
  getSimOrg,
  getSimMember,
  checkCode,
  getSimOrgActSimpleList,
} from '@/api/organization'

const state = {
  memberSimList: {},
}

const mutations = {
  SET_MEMBERSIMLIST: (state, data) => {
    state.memberSimList = data
  }
}

const actions = {
  // 添加部门
  addOrganizationAct({commit}, params) {
    return new Promise((resolve, reject) => {
      addOrganization(params).then(response => {
        const data = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  
  // 编辑部门
  putOrganizationAct({commit}, params) {
    return new Promise((resolve, reject) => {
      putOrganization(params).then(response => {
        const {data} = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取组织架构列表
  getOrgListAct({commit}, params) {
    return new Promise((resolve, reject) => {
      getOrgList(params).then(response => {
        const {data} = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取部门成员列表
  getMemberListAct({commit}, params) {
    return new Promise((resolve, reject) => {
      getMemberList(params).then(response => {
        const {data} = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 添加部门成员
  addOrgMemberAct({commit}, params) {
    return new Promise((resolve, reject) => {
      addOrgMember(params).then(response => {
        const data = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 编辑部门成员
  editOrgMemberAct({commit}, params) {
    return new Promise((resolve, reject) => {
      editOrgMember(params).then(response => {
        const data = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 删除部门成员
  delOrgMemberAct({commit}, params) {
    return new Promise((resolve, reject) => {
      delOrgMember(params).then(response => {
        const {data} = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 导入通讯录
  importMailListACT({commit}, params) {
    return new Promise((resolve, reject) => {
      importMailList(params).then(response => {

        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取部门简单列表
  getSimOrgAct({commit}, params) {
    return new Promise((resolve, reject) => {
      getSimOrg(params).then(response => {
        const {data} = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },  
  
  // 获取部门简单列表
  getSimOrgActSimpleList({commit}, params) {
    return new Promise((resolve, reject) => {
      getSimOrgActSimpleList(params).then(response => {
        const {data} = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 调整部门
  changeOrganizationAct({commit}, params) {
    return new Promise((resolve, reject) => {
      changeOrganization(params).then(response => {
        const data = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取部门成员列表
  getSimMemberAct({commit}, params) {
    return new Promise((resolve, reject) => {
      getSimMember(params).then(response => {
        const {data} = response
        commit('SET_MEMBERSIMLIST', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },  
  
  
  checkCode({commit}, params) {
    return new Promise((resolve, reject) => {
      checkCode(params).then(response => {
        if(response.code==200){
          const {data} = response
          commit('SET_MEMBERSIMLIST', data)
          resolve(data)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

